<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="分类" prop="bannerType">
    </el-form-item>
    <el-form-item label="banner背景色" prop="bannerColourValue" v-if="this.dataForm.bannerType === '0'">
      <el-color-picker v-model="dataForm.bannerColourValue"></el-color-picker>
    </el-form-item>
    <el-form-item label="图片名称" prop="img">
      <!-- <el-input v-model="dataForm.imgName" placeholder="图片名称"></el-input> -->
      <el-upload
      :action="this.$http.adornUrl(`/banner/uploadBanner?token=${this.$cookie.get('token')}`)"
      :limit="1"
      list-type="picture-card"
      :file-list="dataForm.img"
      :on-preview="handlePreview"
      :on-success="handleSuccess"
      :on-remove="handleRemove"
      :on-exceed="onExceed">
      <i class="el-icon-plus"></i>
      </el-upload>
      <el-dialog :visible.sync="dialogVisible" :append-to-body='true'>
        <img width="100%" :src="dialogImageUrl" alt="">
      </el-dialog>
    </el-form-item>
    <!-- <el-form-item label="图片路径" prop="imgPath">
      <el-input v-model="dataForm.imgPath" placeholder="图片路径"></el-input>
    </el-form-item> -->
    <el-form-item label="排序" prop="seq">
      <el-input type="number" style="width: 100px" v-model="dataForm.seq" placeholder="排序，越大越靠前"></el-input>
    </el-form-item>
    <el-form-item label="跳转类型" prop="jumpType">
      <el-row  style="display: flex;">
        <el-select v-model="dataForm.jumpType" placeholder="分类" value="" style="margin-right: 30px">
          <el-option label="商品分类" :value="0" ></el-option>
          <el-option label="联盟店分类" :value="1" ></el-option>
          <el-option label="活动" :value="2" ></el-option>
          <el-option label="外链" :value="3" ></el-option>
          <el-option label="店铺" :value="4" ></el-option>
          <el-option label="专题" :value="5" ></el-option>
          <el-option label="无跳转" :value="6" ></el-option>
          <el-option label="商品" :value="7" ></el-option>
        </el-select>
        <el-input v-model="dataForm.url" placeholder="请输入跳转类型链接"></el-input>
      </el-row>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input type="textarea" v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dialogImageUrl: '',
        dialogVisible: false,
        dataForm: {
          id: 0,
          bannerType: '',
          imgName: '',
          imgPath: '',
          img: [],
          jumpType: '',
          seq: '',
          url: '',
          remark: '',
          bannerColourValue: null,
          bannerTypeOptions: [{
            value: 0,
            label: '首页Banner'
          }, {
            value: 1,
            label: '首页今日必买'
          }, {
            value: 2,
            label: '首页今日抢购'
          }, {
            value: 3,
            label: '超级大玩家页Banner'
          }, {
            value: 4,
            label: '首页分类活动图'
          }]
        },
        dataRule: {
          bannerType: [
            { required: true, message: '分类不能为空', trigger: 'blur' }
          ],
          imgName: [
            { required: true, message: '图片名称不能为空', trigger: 'blur' }
          ],
          imgPath: [
            { required: true, message: '图片路径不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      // 图片预览
      handlePreview (file) {
        this.dialogImageUrl = file.url
        this.dialogVisible = true
      },
      // 文件上传成功的钩子函数
      handleSuccess (res, file, fileList) {
        this.$message({
          type: 'info',
          message: '图片上传成功',
          duration: 6000
        })
        if (res.code === 0) {
          this.dataForm.img.push({name: 'tu', url: res.data})
        }
        // console.log(this.dataForm.img)
      },
      // 删除文件之前的钩子函数
      handleRemove (file, fileList) {
        // debugger
        for (var i = 0; i < this.dataForm.img.length; i++) {
          var picc = this.dataForm.img[i]
          if (picc.url === file.url) {
            this.dataForm.img.splice(i, 1)
          }
        }
        this.$message({
          type: 'info',
          message: '已删除原有图片',
          duration: 6000
        })
      },
      // 上传的文件个数超出设定时触发的函数
      onExceed (files, fileList) {
        this.$message({
          type: 'info',
          message: '最多只能上传1个图片',
          duration: 6000
        })
      },
      init (id) {
        this.dataForm.bannerColourValue = null
        this.dataForm.url = ''
        this.dataForm.remark = ''
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/banner/getBannerById/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.bannerType = data.data.bannerType
                var pic = data.data.imgPath + '/' + data.data.imgName
                this.dataForm.img.push({name: 'tu', url: 'http://image.yilianbaihui.cn/consumer/' + pic})
                this.dataForm.jumpType = data.data.jumpType
                this.dataForm.url = data.data.url
                this.dataForm.seq = data.data.seq
                this.dataForm.remark = data.data.remark
                this.dataForm.bannerColourValue = data.data.bannerColourValue
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/banner/${!this.dataForm.id ? 'addBanner' : 'modifyBannerById'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'bannerType': this.dataForm.bannerType,
                'imgName': this.dataForm.img[0].url.substring(this.dataForm.img[0].url.lastIndexOf('/') + 1, this.dataForm.img[0].url.length),
                'imgPath': 'banner',
                'jumpType': this.dataForm.jumpType,
                'url': this.dataForm.url,
                'seq': this.dataForm.seq,
                'remark': this.dataForm.remark,
                'bannerColourValue': this.dataForm.bannerColourValue
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
